package com.rz.htk.order.task;

import com.rz.htk.order.entity.OrderTrack;
import com.rz.htk.order.service.IOrderTrackService;
import com.rz.htk.order.service.IOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;

import java.util.ArrayList;
import java.util.List;

public class YinSuTrackGetTask implements PageProcessor {
    private Logger log = LoggerFactory.getLogger("YinSuTrackGetTask");
    private Site site = Site.me().setRetryTimes(1).setSleepTime(1000);

    private String afterCode;
    private IOrderService IOrderService;
    private IOrderTrackService orderTrackService;
    public YinSuTrackGetTask(String afterCode, IOrderService IOrderService, IOrderTrackService orderTrackService){
        this.afterCode = afterCode;
        this.IOrderService = IOrderService;
        this.orderTrackService = orderTrackService;
    }

    @Override
    public void process(Page page) {
        Html html =page.getHtml();
        Selectable gridTable = html.$("#GridView_Pub");
        if(gridTable!=null && !StringUtils.isEmpty(gridTable.get())){
            boolean finished = false;
            List<String> tds = gridTable.$("td").all();
            int size = tds.size() / 3;
            List<OrderTrack> orderTracks = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                OrderTrack orderTrack = new OrderTrack();
                int k = i * 3;
                String scanDateTimeTd = tds.get(k);
                String scanAddressTd = tds.get(k+1);
                String scanInfoTd = tds.get(k+2);
                orderTrack.setAfterCode(afterCode);
                orderTrack.setScanAddr(getValue(scanAddressTd));
                String time = getValue(scanDateTimeTd);
                time = time.replaceAll("/","-");
                orderTrack.setScanDate(time);
                orderTrack.setScanInfo(getValue(scanInfoTd));
                orderTracks.add(orderTrack);
                if(scanInfoTd.contains("已簽收")){
                    finished = true;
                }
            }
            if(orderTracks.size()>0){
                orderTrackService.deleteByAfterCode(afterCode);
                orderTrackService.saveBatch(orderTracks);
                if(finished){
                    IOrderService.updateTrackSuccess(afterCode);
                }
            }else{
                orderTrackService.deleteByAfterCode(afterCode);
            }
        }else{
            orderTrackService.deleteByAfterCode(afterCode);
            log.error("抓取音速轨迹 afterCode={},错误内容={}",afterCode,html.get());
        }
    }

    @Override
    public Site getSite() {
        return site;
    }


    private String getValue(String node){
//        int beginIndex= node.indexOf(">");
//        int endIndex= node.lastIndexOf("<");
        int beginIndex = node.indexOf("<font color=\"Black\">");
        int endIndex= node.lastIndexOf("</font>");
//        System.out.println(text.substring(beginIndex+20,endIndex));
        return node.substring(beginIndex+20,endIndex);
    }

}
